package Model;

import java.util.ArrayList;
import java.util.HashMap;

import View.BarChart;

import parser.RequestParser;
import prjClasses.Rating;
import prjClasses.Team;

public class TeamsRating {
	private final String team = "team";
	private final String rating = "rating";
	
	public void createRatingTeamsChart() {
		ArrayList<Object> allTeams = getAllTeams();
		int[] victories = new int[allTeams.size()];
		String[] teamsNames = new String[allTeams.size()];
		for (int i = 0; i < allTeams.size(); i++) {
			Team team = (Team) allTeams.get(i);
			teamsNames[i] = team.getName();
			int ratingId = team.getRatingId();
			Rating ratingByTeam = getRatingById(ratingId);
			victories[i] = ratingByTeam.getVictory();
		}
		createChart(teamsNames, victories);
	}
	
	private void createChart(String[] teamsNames, int[] victories) {
		BarChart chart = new BarChart();
		chart.setTitle("Numbers of wins");
		chart.setNameXAxis("teams");
		chart.setNameYAxis("wins");
		chart.showChart(teamsNames, victories);
	}
	
	private ArrayList<Object> getAllTeams() {
		RequesToService service=new RequesToService();
		HashMap<String, ArrayList<Object>> allObj = getObjectMap(service.getRequestAllTeams());
		return allObj.get(team);
	}

	private Rating getRatingById(int id) {
		RequesToService service=new RequesToService();
		HashMap<String, ArrayList<Object>> allObj = getObjectMap(service.getTeamRatingById(id));
		ArrayList<Object> arrObj = allObj.get(rating);
		if (arrObj.isEmpty())
			System.out.println("[X] ID not found! ID=" + id);
		if (arrObj.size() > 1)
			System.out.println("[X] ID not unique! ID=" + id);
		return (Rating) arrObj.get(0);
	}
	
	
	private HashMap<String, ArrayList<Object>> getObjectMap(String[][] data) {
		RequestParser par = new RequestParser();
		return par.parseRequest(data);
	}
	
	
}
